﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ITS.DAL.Contents;
using ITS.Core.Domain.Contents;
using ITS.Infrastructure.Constants;

namespace ITS.DAL.Concrete.EF.Contents
{
    public class EFContentRepository: IContentRepository
    {
        private readonly EnterpriseContext _context = new EnterpriseContext();
        public ContentCategory ContentCategory(int categoryID)
        {
            return _context.ContentCategories.SingleOrDefault(o => o.CategoryID == categoryID);
        }


        public IQueryable<ContentSubCategory> SubCategoriesByCategory(int categoryID)
        {
            return _context.ContentSubCategories.Where(o => o.CategoryID == categoryID);
        }


        public ContentSubCategoryDescription DefaultSubCategoryDescription(int subCategoryID)
        {
            return _context.ContentSubCategoryDescriptions.SingleOrDefault(o => o.SubCategoryID == subCategoryID && o.LanguageID == CommonConstants.DefaultWebLanguage);
        }


        public void SaveContentItem(ContentItem contentItem)
        {
            _context.ContentItems.Add(contentItem);
            _context.SaveChanges();
        }

        public void SaveContentDescription(ContentItemDescription description)
        {
            _context.ContentItemDescriptions.Add(description);
            _context.SaveChanges();
        }


        public ContentCategory ContentCategoryOfItem(int contentID)
        {
            throw new NotImplementedException();
        }


        public ContentItem ContentItem(int contentID)
        {
            return _context.ContentItems.SingleOrDefault(o => o.ContentID == contentID);
        }


        public ContentItemDescription ContentItemDescription(Guid descriptionID)
        {
            return _context.ContentItemDescriptions.SingleOrDefault(o => o.DescriptionID == descriptionID);
        }


        public void UpdateContentItemDescription(ContentItemDescription description)
        {
            _context.Entry(description).State = System.Data.EntityState.Modified;
            _context.SaveChanges();
        }

        public void AddContentItemDescription(ContentItemDescription description)
        {
            _context.ContentItemDescriptions.Add(description);
            _context.SaveChanges();
        }


        public ContentItemDescription ContentItemDescription(int contentID, string languageID)
        {
            ContentItemDescription description = _context.ContentItemDescriptions.SingleOrDefault(o => o.ContentID == contentID && o.LanguageID == languageID);

            if (description == null)
            {
                description = _context.ContentItemDescriptions.FirstOrDefault(o => o.ContentID == contentID);
            }

            return description;
        }


        public IList<ContentCategory> ContentCategories()
        {
            return _context.ContentCategories.ToList();
        }


        public IList<ContentItem> Search(string keyword, int subCategoryID)
        {
            return _context.ContentItems.Where(o => o.SubCategoryID == subCategoryID).ToList();
        }
    }
}
